define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'approves/index' + location.search,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'type_text', title: __('Type')},
                        {field: 'approves_text', title: __('Users'), operate:false, formatter: Table.api.formatter.label},
                        {field: 'status', title: __('Status'), searchList: {"cancel":__('Status cancel'),"wait":__('Status wait'),"ing":__('Status ing'),"refuse":__('Status refuse'),"pass":__('Status pass')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        action: function () {
            // 初始化表格参数配置
            var id = $('#row_id').val();
            Table.api.init({
                extend: {
                    index_url: 'approves/action/ids/'+ id,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'oper.nickname', title: __('Users')},
                        {field: 'oper.title.name', title: __('Oper_title')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'remark', title: __('Remark')},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        reimburse_detail: function () {
            // 初始化表格参数配置
            var id = $('#row_id').val();
            Table.api.init({
                extend: {
                    index_url: 'approves/reimburse_detail/ids/'+ id,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'title', title: __('Title')},
                        {field: 'intro', title: __('Intro')},
                        {field: 'price', title: __('Price')},
                        {field: 'attachment', title: __('Attachment'), events: Table.api.events.attachment, formatter: Table.api.formatter.files, operate: true},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        purchase_detail: function () {
            // 初始化表格参数配置
            var id = $('#row_id').val();
            Table.api.init({
                extend: {
                    index_url: 'approves/purchase_detail/ids/'+ id,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'name', title: __('Part_name')},
                        {field: 'num', title: __('Part_num')},
                        {field: 'form', title: __('Part_form')},
                        {field: 'images', title: __('Part_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        leave: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'approves/leave' + location.search,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "nickname").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User'),operate:false},
                        {field: 'user_id', title: __('User_id'),formatter: Table.api.formatter.search,visible:false},
                        {field: 'leave.leave_type_text', title: __('Leave_type'),operate:false},
                        {field: 'leave.leave_days', title: __('Leave_days'),operate:false},
                        {field: 'leave.leave_reason', title: __('Leave_reason'),operate:false},
                        {field: 'leave.leave_time_text', title: __('Leave_time_text'),operate:false},
                        {field: 'approves_text', title: __('Users'), operate:false, formatter: Table.api.formatter.label,operate:false},
                        {field: 'status', title: __('Status'), searchList: {"cancel":__('Status cancel'),"wait":__('Status wait'),"ing":__('Status ing'),"refuse":__('Status refuse'),"pass":__('Status pass')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('审批记录'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-wpforms',
                                    url: 'approves/action',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);    //移除 编辑按钮
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        reimburse: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'approves/reimburse' + location.search,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "nickname").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User'),operate:false},
                        {field: 'user_id', title: __('User_id'),formatter: Table.api.formatter.search,visible:false},
                        {field: 'reimburse.reimburse_name', title: __('Reimburse_name'),operate:false},
                        {field: 'reimburse.reimburse_date', title: __('Reimburse_date'),operate:false},
                        {field: 'reimburse.reimburse_price', title: __('Reimburse_price'),operate:false},
                        {field: 'approves_text', title: __('Users'), operate:false, formatter: Table.api.formatter.label,operate:false},
                        {field: 'status', title: __('Status'), searchList: {"cancel":__('Status cancel'),"wait":__('Status wait'),"ing":__('Status ing'),"refuse":__('Status refuse'),"pass":__('Status pass')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('报销详情'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'approves/reimburse_detail',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('审批记录'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-wpforms',
                                    url: 'approves/action',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);    //移除 编辑按钮
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        purchase: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'approves/purchase' + location.search,
                    add_url: 'approves/add',
                    edit_url: 'approves/edit',
                    del_url: 'approves/del',
                    multi_url: 'approves/multi',
                    import_url: 'approves/import',
                    table: 'approves',
                }
            });

            var table = $("#table");
            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user_id']", form).addClass("selectpage").data("source", "user/user/index").data("primaryKey", "id").data("field", "nickname").data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user.nickname', title: __('User'),operate:false},
                        {field: 'user_id', title: __('User_id'),formatter: Table.api.formatter.search,visible:false},
                        {field: 'purchase.task_id', title: __('Task_id'),operate:false},
                        {field: 'task.report_number', title: __('Report_number'),operate:false},
                        {field: 'status', title: __('Status'), searchList: {"cancel":__('Status cancel'),"wait":__('Status wait'),"ing":__('Status ing'),"refuse":__('Status refuse'),"pass":__('Status pass')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('采购信息'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'approves/purchase_detail',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                                {
                                    name: 'detail',
                                    title: __('审批记录'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-wpforms',
                                    url: 'approves/action',
                                    callback: function (data) {
                                        //Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-edit", null);    //移除 编辑按钮
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'approves/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'approves/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'approves/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
